<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Systemadmin_user_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
    }
    
	/**
	 * ��̨��½��֤
	 * @param string $user �û���
	 * @param string $password ����
	 * @return array/false
	 */
	function check_login($user = '', $password = ''){
		$password = md5(md5($password));
		$sql = "select id,adminname,type_id,status,login_time,login_ip from rsh_system_admin_user where status=1 and adminname='{$user}' and password='{$password}' limit 1";
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0){
			return $query->row_array();
		}else{
			return false;
		}
	}

	/**
	 * ��ȡ����Ա����Ȩ��
	 * @param int ����Ա��ɫ����ID
	 * @return array/false
	 */
	function get_user_adminlist($typeid){
		$sql= "select name,module from rsh_system_admin_type where id='{$typeid}'";
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0){
			return $query->row_array();
		}else{
			return false;
		}
	}
	function bar(){
		$res = array();
		 $admin = $this->session->userdata('admin_user');

		 if($admin != null){
		 	$ns_key = $this->mycache->get("foo_bar");		 	
		 	//生成一个用来保存 namespace 的 key  
		 	if($ns_key == null)   {
		 		$this->mycache->set("foo_bar",time());
		 		$ns_key = $this->mycache->get("foo_bar");
		 	}
		 	$header_list = array();
		 	if($this->mycache->get_array("foo_".$ns_key."_".$admin['id']) == null ){
		 		$this->config->load('admin_type',TRUE);
				$admintype = $this->config->item('admintype', 'admin_type');
				$this->load->model('systemadmin_user_model');
				$admin_list = $this->systemadmin_user_model->get_user_adminlist($admin['type_id']);//获取管理员权限
				$type_arr = explode(',',$admin_list['module']);//获取管理员权限
				foreach ($admintype as $k=>$v){
					
					$keyname = array_keys($v['list']);
					$repeat_arr = array_intersect ( $keyname, $type_arr);
					$list = array();
					foreach($repeat_arr as $lk => $lv){
						$list[$lk] = $v['list'][$lv];
					}
					if(count($repeat_arr)>0) $header_list[$k] = array('name'=>$v['name'],'module'=>$v['module'],'list'=>$list);
				}
				$this->mycache->set_array("foo_".$ns_key."_".$admin['id'], $header_list,21600);
		 	}
		 	else $header_list = $this->mycache->get_array("foo_".$ns_key."_".$admin['id']);
		 	//var_dump($header_list["global"]);exit;
			$res['list'] = $header_list;
			$res['admin'] = $admin;
		 }
		 return $res;
	}
}

